﻿using Business.Core;
using Business.Core.Annotations;
using Business.Core.Result;
using Business.Data;
using Common;
using DataModel;
using LinqToDB;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ProjectManager.User;
using ProjectManager.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HolidayManagerSys.backoffice.project
{
    /// <summary>
    /// 节假日
    /// </summary>
    public class HolidayMerber : Common.BusinessBase
    {
        
        [Doc("获取节假日列表", Group = "节假日管理")]
        public virtual async Task<dynamic> HolidayList(int page, int limit, int year,int month)
        {
            using (var con = Data.DB.GetConnection())
            {
                var list = con.MANAGER_HOLIDAY.AsQueryable();
                list = list.Where(p => p.ACTIVE == 1);
                if (year > 0) 
                    list = list.Where(p => p.YEAR==(year));
                if (month > 0)
                    list = list.Where(p=>p.MONTH == month);
                list = list.OrderBy(p=>p.HOLIDAY);

                var pageData = list.GetPaging(page, limit);
                var result = new { code = 0, msg = "", count = pageData.Count, data = pageData.Data };
                IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                return JsonConvert.SerializeObject(result, Formatting.Indented, timeFormat);
            }
        }

        [Doc("新增节假日", Group = "节假日管理")]
        public virtual async Task<dynamic> AddHoliday(Arg<holidaynew> arg)
        {
            var flag = 0;
            try
            {
                
                //计算假期开始时间到结束的天数
                TimeSpan ts = arg.Out.endTime - arg.Out.startTime;
 
                using (var con = Data.DB.GetConnection())
                {
                    for (int i = 0; i <= ts.TotalDays; i++)
                    {
                        MANAGER_HOLIDAY oHoliday = new MANAGER_HOLIDAY();
                        oHoliday.ID = Guid.NewGuid().ToString();
                        AuthInfo authinfo = Logins.Authorize();
                        oHoliday.UPDATEBY = authinfo.userid;
                        oHoliday.ACTIVE = 1;
                        oHoliday.CREATETIME = DateTime.Now;
                        oHoliday.UPDATETIME = DateTime.Now;
                        oHoliday.HOLIDAY = DateTime.Now;
                        oHoliday.HOLIDAY = arg.Out.startTime.AddDays(i);
                        oHoliday.YEAR = arg.Out.startTime.AddDays(i).Year;
                        oHoliday.MONTH = arg.Out.startTime.AddDays(i).Month;
                        //获取日期是星期几，判断是否为工作日
                        oHoliday.WEEK = DateOperation.validateWeek(arg.Out.startTime.AddDays(i));
                        oHoliday.ISWORKDAY = (oHoliday.WEEK == "六" || oHoliday.WEEK == "日") ? 1 : 0;
                        oHoliday.DATETYPE = Convert.ToInt32(arg.Out.datetype);
                        oHoliday.DETAIL = arg.Out.detail;
                        flag += await con.InsertAsync(oHoliday);
                    }
                }

            }
            catch (Exception ex)
            {
                 
            }
            //入库的用户
            return flag;
        }

        [Doc("修改节假日", Group = "节假日管理")]
        public virtual async Task<dynamic> UpdateHoliday(Arg<holidaynew> arg)
        {
            var flag = 0;
            try
            {
                
                using (var con = Data.DB.GetConnection())
                {
                    var oHoliday = con.MANAGER_HOLIDAY.Where(c=>c.ID == arg.Out.id).FirstOrDefault();
                    if (oHoliday == null)
                        return new { flag = flag,message = "未查询到数据，数据或已被删除"};
                    oHoliday.HOLIDAY = arg.Out.holiday;
                    oHoliday.YEAR = arg.Out.holiday.Year;
                    oHoliday.MONTH = arg.Out.holiday.Month;
                    //获取日期是星期几，判断是否为工作日
                    oHoliday.WEEK = DateOperation.validateWeek(arg.Out.holiday);
                    oHoliday.ISWORKDAY = (oHoliday.WEEK == "六" || oHoliday.WEEK == "日") ? 1 : 0;
                    oHoliday.DATETYPE =Convert.ToInt32(arg.Out.datetype);
                    oHoliday.UPDATETIME = DateTime.Now;
                    oHoliday.DETAIL = arg.Out.detail;
                    AuthInfo authinfo = Logins.Authorize();
                    oHoliday.UPDATEBY = authinfo.userid;
                    flag = await con.UpdateAsync(oHoliday);
                    
                }
            }
            catch (Exception ex)
            {
                 
            }
            return new { flag = flag, message = flag > 0 ? "修改成功" : "修改失败，请稍后重试" };
        }

        [Doc("获取节假日详情", Group = "节假日管理")]
        public virtual async Task<IResult> GetHolidayInfo(string id)
        {
            using (var con = Data.DB.GetConnection())
            {
                var data = con.MANAGER_HOLIDAY.Where(x => x.ID == id).FirstOrDefault();
                if (null == data)
                {
                    return ResultCreate(-111);
                }
                return ResultCreate(data);
            }
        }

        [Doc("删除节假日", Group = "节假日管理")]
        public virtual async Task<dynamic> DeleteHoliday(string delIds)
        {
            using (var con = Data.DB.GetConnection())
            {
                var flag = 0;
                if (!String.IsNullOrEmpty(delIds))
                {
                    var idArray = delIds.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if (idArray.Length > 0)
                    {
                        foreach (var item in idArray)
                        {
                            var oModel = con.MANAGER_HOLIDAY.Where(c => c.ID == item).FirstOrDefault();
                            if (oModel != null)
                            {
                                oModel.ACTIVE = 0;
                                flag += await con.UpdateAsync(oModel);
                            }
                        }
                    }
                }

                return (flag);
            }
        }

        public class holidaynew
        {
            public DateTime startTime { get; set; }
            public DateTime endTime { get; set; }
            public string detail { get; set; } 
            public string datetype { get; set; }
            public string id { get; set; }
            public DateTime holiday { get; set; }
        }
    }
}
